﻿using Models;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.UI.WebControls.WebParts;
using wnet.admin.Models.dto;
using wnet.admin.wentcommon;
using wnet.admin.Models.business;




namespace wnet.admin.Controllers.pcapi
{

    [Route("business/admininfo/{action}")]
    public class admininfoController : ApiController
    {

        private readonly ISqlSugarClient _sqlSugarClient;
        public admininfoController()
        {
            _sqlSugarClient = SqlSugarConfig.CreateClient();
        }

        /// <summary>
        /// 管理员信息获取
        /// </summary>
        /// <returns></returns>
        [TokenAuth]
        [HttpGet]
        public object getInfo()
        {
            int uid = tools.getuid();
            var user = _sqlSugarClient.Queryable<sys_user>().Where(q => q.id == uid).First();
            //前端校验按钮权限使用
            //角色集合 eg: admin,yunying,common
            List<string> roles = new List<string>();
            roles.Add("admin");
            //权限集合 eg *:*:*,system:user:list
            List<string> permissions = new List<string> { "*:*:*" };

            return new { code = 200, msg = "成功", data = new { user, roles, permissions } };
        }



        /// <summary>
        ///我的菜单 不同角色获取不同菜单
        /// </summary>
        /// <returns></returns>
        [TokenAuth]
        [HttpGet]
        public object getmenulist()
        {
            List<int> menuids = new List<int>();
            int uid = tools.getuid();
            try
            {
                var user = _sqlSugarClient.Queryable<sys_user>().Where(q => q.id == uid).First();
                var roleids = user.roleids.Split(',').Select(int.Parse).ToList();
                var rolelist = _sqlSugarClient.Queryable<sys_role>().Where(q => q.status == 1 && roleids.Contains(q.id)).ToList();


                foreach (var item in rolelist)
                {
                    var temp = item.menuids.Split(',').Select(int.Parse).ToList();
                    menuids.AddRange(temp);
                }
            }
            catch (Exception)
            {

                
            }
            var list = _sqlSugarClient.Queryable<sys_menu>().Where(q => q.status == 1 && menuids.Contains(q.id)).OrderByDescending(q => q.sorder).ToList();
            List<sys_menudto> dlist = new List<sys_menudto>();

            foreach (var item in list.Where(q => q.pid == 0))
            {
                sys_menudto sys_menudto = new sys_menudto();
                sys_menudto.name = item.name;
                sys_menudto.surl = item.surl;
                sys_menudto.id = item.id;

                var sublist = list.Where(q => q.pid == item.id).ToList();
                if (sublist.Count > 0)
                {
                    sys_menudto.subitems = new List<sys_menudto>();
                    foreach (var row in sublist)
                    {
                        sys_menudto sys_menudto2 = new sys_menudto();
                        sys_menudto2.name = row.name;
                        sys_menudto2.surl = row.surl;
                        sys_menudto2.id = row.id;
                        sys_menudto.subitems.Add(sys_menudto2);
                    }
                }

                dlist.Add(sys_menudto);
            }

            sys_menudto sys_menudto1 = new sys_menudto();
            sys_menudto1.name = "控制台";
            sys_menudto1.surl = "/desktop";
            sys_menudto1.id = 0;
            dlist.Insert(0, sys_menudto1);

            return new { code = 200, msg = "成功", data = dlist };
        }



        /// <summary>
        /// 更新管理员信息
        /// </summary>
        /// <returns></returns>
        [TokenAuth]
        [HttpPost]
        public object updateuserinfo(sys_user parm)
        {

            var user = _sqlSugarClient.Queryable<sys_user>().Where(q => q.id == parm.id).First();

            user.updatetime = DateTime.Now;


            user.name = parm.name;
            user.avatar = parm.avatar;

            if (!string.IsNullOrEmpty(parm.loginpwd))
            {
                user.loginpwd = tools.GetMD5Hash(user.loginpwd); ;
            }


            var response = _sqlSugarClient.Updateable<sys_user>(user).ExecuteCommand();
            var data = new { code = 200, msg = "成功", data = response };

            return Json(data, new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            });
        }





    }
}
